package 读写状态的判断及长连接要素;


import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class MyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if(evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent)evt;

            String eventType = null;

            switch (event.state()) {//event.state()返回的IdleState是一个枚举类。
                case READER_IDLE:
                    eventType = "读空闲";//无读的状态。
                    break;
                case WRITER_IDLE:
                    eventType = "写空闲";//无写的状态。
                    break;
                case ALL_IDLE:
                    eventType = "读写空闲";//无读无写时的状态。
                    break;
            }
            System.out.println(ctx.channel().remoteAddress() + "超时事件： " + eventType);
            ctx.channel().close();
        }
    }
}
